package com.istudy.service.imp;

import com.istudy.dao.impl.UserImp;
import com.istudy.dao.interfaces.UserDao;
import com.istudy.dao.xyjdao.UserIntDao;
import com.istudy.dao.xyjdao.impl.UserImplDao;
import com.istudy.entity.User;
import com.istudy.entity.UserType;
import com.istudy.service.interfaces.UserService;
import pers.dzk.jdk.dao.DataSource;
import pers.dzk.jdk.entity.PageBean;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImp implements UserService {
    UserIntDao userIntDao = new UserImplDao(DataSource.getConnection());

    @Override
    public boolean repetitionLoginName(User user){
        Connection connection = DataSource.getConnection();
        UserDao userDao = new UserImp(DataSource.getConnection());
        User user1 = userDao.selectUserByLoginName(user);
        try {
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return user1 != null;
    }

    @Override
    public PageBean<User> userManage(int pageNo, int pageSize) {
        Connection connection = DataSource.getConnection();
        UserDao us = new UserImp(connection);
        PageBean<User> pageBean = new PageBean();
        pageBean.setCurrPageNo(pageNo);
        pageBean.setPageSize(pageSize);
        PageBean<User> data = us.select(pageBean);
        try {
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }

    @Override
    public User userToUpdate(User user) {
        Connection connection = DataSource.getConnection();
        UserDao ud = new UserImp(connection);
        User user2 = ud.selectByUserId(user);
        try {
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return user2;
    }

    @Override
    public boolean userUpdate(User user) {
        Connection connection = DataSource.getConnection();
        UserDao ud = new UserImp(connection);
        boolean boo = ud.updateUser(user) != -1;
        try {
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return boo;
    }

    @Override
    public User getUserById(int id) {
        Connection connection = DataSource.getConnection();
        UserDao ud = new UserImp(connection);
        User user = ud.getUserById(id);

        try {
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public boolean updateMoney(User user) {
        Connection connection = DataSource.getConnection();
        UserDao ud = new UserImp(connection);
        boolean boo = ud.updateUserByUserId(user) != -1;
        try {
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return boo;
    }

    @Override
    public boolean deleteUser(User user) {
        Connection connection = DataSource.getConnection();
        UserDao ud = new UserImp(connection);
        boolean boo = ud.deleteUserByUserId(user) != -1;
        try {
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return boo;
    }

    @Override
    public List<UserType> selectUserType() {
        Connection connection = DataSource.getConnection();
        UserDao ud = new UserImp(connection);
        List<UserType> list = ud.selectUserType();
        try {
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean register(User user) {
            {
                boolean b1 = user.getUserPassword() != null && user.getUserLoginName() != null && user.getUserName() != null;//数据完整性
                if(!b1){
                    return false;
                }
                boolean b2 = user.getUserPassword().length() > 7 && user.getUserPassword().length() < 19;//密码长度>7&<19
                boolean b3 = user.getUserLoginName().length() > 7 && user.getUserLoginName().length() < 11;//登录名长度>7&<11
                boolean b4 = user.getUserName().length() < 11;//用户名长度小于11
                if(!(b1 && b2 && b3 && b4)){
                    return false;
                }
            }//双端验证
            user.setUserType(2);
            user.setUserMoney(100);
            Connection connection = DataSource.getConnection();
            UserDao userDao = new UserImp(connection);
            int i = userDao.insertUser(user);
            try {
                connection.close();
            }catch (Exception e){
                e.printStackTrace();
            }
            boolean register = i != -1;
            if (register) {
                return true;
            } else {
                return false;
            }
    }

    @Override
    public User login(User user) {
        User user1;
        Connection connection = DataSource.getConnection();
        UserDao userdao = new UserImp(connection);
        user1 = userdao.selectUserByUserLoginNameAndUserPassword(user);
        if(user1.getUserType() == 1){
            user1 = null;
        }
            try {
                connection.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        return user1;
    }


    /**
     * 根据指定用户进行相应扣费
     *
     * @param userId       用户id
     * @param videoConsume 扣费金额
     * @return 是否成功
     */
    @Override
    public boolean UserDeduction(String userId, String videoConsume) {
        int userIdInt = 0;
        int videoConsumeInt = 0;
        try {
            userIdInt = Integer.parseInt(userId);
            videoConsumeInt = Integer.parseInt(videoConsume);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("User业务逻辑层扣费异常，无法转换参数");
        }
        try {
            int i = userIntDao.UserDeduction(userIdInt, videoConsumeInt);
            if (i == 1) {
                return true;//扣费成功
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;//扣费失败
    }
}
